<template>
  <div class="login-container">
    <div class="login-left">
      <div class="logo" @click="() => $router.push({ path: '/' })">
        <svg-icon class="svg-logo" icon="icon-logo" />
        <span class="name">Visualio.</span>
      </div>
      <div class="login-title">
        <span class="title-big">产品设计一体化工具</span>
        <span class="title-small">
          一站式完成白板、原型、设计、交付和运维，把产品设计得更简单。
        </span>
      </div>
    </div>
    <div class="login-from-box">
      <div class="login-from-title">
        <div
          class="title"
          :style="{
            color: loginIndex ? '#000' : '#409eff',
          }"
          @click="changeLogin(0)"
        >
          密码登录
        </div>
        <div
          class="title"
          :style="{
            color: loginIndex ? '#409eff' : '#000',
          }"
          @click="changeLogin(1)"
        >
          扫码登陆
        </div>
        <div
          class="title-bottom"
          :style="{
            transform: `translateX(${loginIndex * 160}px)`,
          }"
        />
      </div>
      <div class="login-from-content">
        <div class="sign-box" v-show="loginIndex == 1">
          <div id="qrCode" ref="qrCode" @click="changeQRcode"></div>
        </div>
        <div class="sign-box" v-show="loginIndex == 0">
          <div class="mobile-input-box">
            <input
              class="input--box"
              placeholder="请输入账号"
              v-model="loginForm.username"
            />
            <div class="input--error-tip" style="display: none"></div>
          </div>
          <div class="mobile-input-box">
            <input
              class="input--box"
              placeholder="请输入密码"
              maxlength="100"
              type="password"
              v-model="loginForm.password"
            />
          </div>
          <div class="btn-next" @click="submit">登录</div>
          <div class="sign-account-btn">
            <span>注册账号</span>
            <span>忘记密码</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import QRCode from "qrcodejs2";
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginIndex: 0,
      qrcode: null,
    };
  },
  mounted() {},
  methods: {
    submit() {
      this.$router.push({
        path: "/Home",
      });
    },
    changeLogin(data) {
      this.loginIndex = data;
      if (data) {
        this.bindQRCode();
      } else {
        this.qrcode.clear();
        this.$refs.qrCode.innerHTML = ""; //将之前的二维码清空
      }
    },
    changeQRcode() {
      this.qrcode.makeCode(this.$methods.uuid()); // make another code.
    },
    bindQRCode() {
      this.$nextTick((_) => {
        const size = 220;
        this.qrcode = new QRCode(this.$refs.qrCode, {
          text: this.$methods.uuid(),
          width: size,
          height: size,
          colorDark: "#000", //二维码颜色
          colorLight: "#fff", //二维码背景色
          correctLevel: QRCode.CorrectLevel.L, //容错率L/M/H
        });
      });
    },
    concurrent(chunkList) {
      let concurrent = 2; // 并发数
      let progress = 0; // 进度
      const concurrentExecution = (list, limit, asyncHandle) => {
        // 递归执行
        let recursion = (arr) => {
          // 执行方法 arr.shift() 取出并移除第一个数据
          return asyncHandle(arr.shift()).then(() => {
            // 数组还未迭代完，递归继续进行迭代
            if (arr.length !== 0) {
              return recursion(arr);
            } else {
              return "finish";
            }
          });
        };
        // 创建新的并发数组
        let listCopy = [].concat(list);
        // 正在进行的所有并发异步操作
        let asyncList = [];
        limit = limit > listCopy.length ? listCopy.length : limit;
        while (limit--) {
          asyncList.push(recursion(listCopy));
        }

        // 所有并发异步操作都完成后，本次并发控制迭代完成
        return Promise.all(asyncList);
      };
      concurrentExecution(chunkList, concurrent, (item) => {
        return new Promise((resolve, reject) => {
          progress++;
          console.log(`${progress}/${chunkList.length}`);
          setTimeout(() => {
            resolve({
              item,
              progress,
            });
          }, 3 * 1000);
        });
      });
    },
  },
};
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: #fff;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-left {
  width: 345px;
  height: 440px;
  margin: 0px 120px;

  .logo {
    font-size: 32px;
    color: #000;
    text-decoration: none;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    justify-content: flex-start;
    align-items: center;

    .name {
      margin: 0 6px;
    }

    .svg-logo {
      width: 56px;
      height: 56px;
    }
  }
  .login-title {
    width: 100%;
    margin-top: 56px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;

    .title-big {
      color: #000;
      font-weight: 700;
      font-size: 36px;
      line-height: 100%;
      margin-bottom: 24px;
    }
    .title-small {
      font-weight: 400;
      font-size: 16px;
      line-height: 180%;
      color: #555557;
    }
  }
}

.login-from-box {
  width: 440px;
  height: 440px;
  background: #fff;
  border: 1px solid #eee;
  box-shadow: 0 22px 50px #d9e0fd5c;
  border-radius: 16px;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  opacity: 0;
  animation: forRight 1s ease forwards;
  animation-delay: 0.28s;
}

@keyframes forRight {
  0% {
    transform: translateX(200px);
  }

  100% {
    transform: translateX(0);
    opacity: 1;
  }
}

.login-from-title {
  position: relative;
  width: 360px;
  height: 52px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  border-bottom: 1px solid #eee;

  .title {
    width: 50%;
    text-align: center;
    cursor: pointer;
  }

  .title-bottom {
    position: absolute;
    bottom: -1px;
    left: 0;
    width: 50%;
    height: 2px;
    border-radius: 2px;
    background: #6166fc;
    transform: translateX(0);
    transition: all 0.58s ease-in-out;
  }
}

.login-from-content {
  width: 360px;
  height: 300px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  position: relative;

  .sign-box {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .mobile-input-box {
    width: 100%;
    height: 50px;
    margin-bottom: 24px;
    position: relative;

    & input {
      width: 100%;
      height: 50px;
      padding: 14px 16px;
      background: #f7f7f9;
      border: 1px solid transparent;
      box-sizing: border-box;
      outline: none;
      color: #19191a;
      border-radius: 6px;
      transition: all 0.25s ease-in-out;
    }
  }

  .btn-next {
    width: 100%;
    height: 50px;
    padding: 14px 0;
    font-size: 16px;
    line-height: 22px;
    text-align: center;
    box-sizing: border-box;
    color: #fff;
    background: #3a6cd1;
    border-radius: 6px;
    cursor: pointer;
    user-select: none;
    font-weight: 600;
    box-shadow: 2px 4px 11px #a4c2ff;

    &:hover {
      background: #6166fc;
    }
  }

  .sign-account-btn {
    width: 100%;
    margin-top: 12px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;

    & span {
      color: #8b8c8f;
      font-size: 12px;
      cursor: pointer;
    }

    & span:hover {
      color: #409eff;
    }
  }
}
</style>
